package com.wz.compilerManual.tree;

import com.wz.compilerManual.Symbol;
import com.wz.compilerManual.add.symbol.*;

public class ParseTree {
    private Node root;

    private Node top;

    public ParseTree(Node root) {
        this.root = root;
        this.top = root;
    }

    public void push(Symbol[] symbols) {
        Node tmpNode = this.top.getRightBrother();
        this.top.setChildrenSize(symbols.length);
        for (int i = 0; i < symbols.length; i++) {
            Node child = new Node(symbols[i], this.top);
            this.top.addChildren(child, symbols.length - 1 - i);
            child.setRightBrother(tmpNode);
            tmpNode = child;
        }
        this.top = tmpNode;
    }

    public Node peek() {
        return this.top;
    }

    public Node pop() {
        Node newNode = this.top;
        this.top = this.top.getRightBrother();
        newNode.setRightBrother(null);
        return newNode;
    }

    public static void main(String[] args) {
        Node node1 = new Node(new S_E());
        ParseTree parseTree = new ParseTree(node1);

        Symbol[] symbols = new Symbol[2];
        symbols[0] = new S_Ee();
        symbols[1] = new S_N();
        parseTree.push(symbols);

        Symbol[] symbols1 = new Symbol[2];
        symbols1[0] = new S_Nn();
        symbols1[1] = new S_number();
        parseTree.push(symbols1);

        System.out.println(parseTree.peek().getSymbol().toString());
    }
}